package main

import (
	"fmt"
	"os"
)

// fmt包实现类似C语言的printf和scanf的格式化I/O，主要分为向外输出内容和获取输入内容两部分
func main() {
	// 向外输出: Print/Fprint/Sprint
	fmt.Print("在终端打印信息")
	name := "何鹏举"
	fmt.Printf("我是: %s\n", name)
	fmt.Println("在终端打印单独一行信息")

	fmt.Fprintln(os.Stdout, "向标准输出写入内容")
	file, err := os.OpenFile("./xxx.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("打开文件错误, err: ", err)
		return
	}
	defer file.Close()
	fmt.Fprintf(file, "往文件夹中写入信息: %s\n", name)

	str := fmt.Sprintf("name: %s", name)
	fmt.Println(str)

	// Errorf 根据format参数生成格式化字符串并返回一个包含改字符串的错误
	err = fmt.Errorf("这是错误 %s", name)
	fmt.Println(err)

	p := &FmtPerson{
		Name: "张三",
		Age:  18,
	}
	fmt.Printf("人员: %v\n", p)  // &{张三 18}
	fmt.Printf("人员: %+v\n", p) // &{Name:张三 Age:18}
	fmt.Printf("人员: %#v\n", p) // &main.Person{Name:"张三", Age:18}

	// 获取输入: Scan, Fscan, Sscan
	var (
		title   string
		age     int
		married bool
	)
	fmt.Scan(&title, &age, &married)
	fmt.Printf("扫描结果 name: %s age: %d married: %t\n", title, age, married)
}

type FmtPerson struct {
	Name string
	Age  int
}
